package com.linkage.jz.bankruptcy.activity.Vote;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.Vote.ProVote;
import com.linkage.jz.bankruptcy.fragment.Vote.ProVoteVotersFragment;
import com.linkage.jz.bankruptcy.fragment.Vote.ProVoteVotersWithGroupFragment;
import com.linkage.jz.bankruptcy.utils.BundleUtils;

import java.util.ArrayList;

/**
 * 表决人详情
 * see https://lanhuapp.com/web/#/item/project/board/detail?pid=19ffdabe-41f1-40c0-91af-37b491d5d596&project_id=19ffdabe-41f1-40c0-91af-37b491d5d596&image_id=26a11528-8dbd-48be-9c4f-6ef466ea789d
 */
public class ProVoteVotersDetailActivity extends ProVoteBaseActivity {

    private ProVote proVote;

    private RadioGroup radioGroup;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    private ViewPager viewPager;
    private FragmentManager fragmentManager;
    private ViewPagerAdapter adapter;
    private ArrayList<Object> items=new ArrayList<Object>();

    private String groupId;

    public static void showVoters(Activity activity, String groupId) {
        Intent intent = new Intent(activity, ProVoteVotersDetailActivity.class);
        intent.putExtra("groupId", groupId);
        activity.startActivity(intent);
    }

    @Override
    public void initData() {
        proVote = BundleUtils.getProVote();
        groupId = getIntent().getStringExtra("groupId");
    }

    @Override
    public void setListener() {
        if(proVote.getGroupInfo() == null) {
            finish();
            return;
        }
        fragmentManager = this.getSupportFragmentManager();
        if(proVote.isGrouped()) {//多分组
            items.add(ProVoteVotersWithGroupFragment.newInstance(1, ""));
            items.add(ProVoteVotersWithGroupFragment.newInstance(2, ""));
            items.add(ProVoteVotersWithGroupFragment.newInstance(3, ""));
        }else {//不分组
            items.add(ProVoteVotersFragment.newInstance(1, groupId));
            items.add(ProVoteVotersFragment.newInstance(2, groupId));
            items.add(ProVoteVotersFragment.newInstance(3, groupId));
        }
        adapter = new ViewPagerAdapter(fragmentManager, items);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0, false);
        viewPager.setOffscreenPageLimit(items.size());

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio1:
                        radioButton1.setTextSize(18);
                        radioButton2.setTextSize(14);
                        radioButton3.setTextSize(14);
                        viewPager.setCurrentItem(0);// 选择某一页
                        break;
                    case R.id.radio2:
                        radioButton1.setTextSize(14);
                        radioButton2.setTextSize(18);
                        radioButton2.setTextSize(14);
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.radio3:
                        radioButton1.setTextSize(14);
                        radioButton2.setTextSize(14);
                        radioButton3.setTextSize(18);
                        viewPager.setCurrentItem(2);// 选择某一页
                        break;
                }
            }
        });
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        radioButton1.setChecked(true);
                        break;
                    case 1:
                        radioButton2.setChecked(true);
                        break;
                    case 2:
                        radioButton3.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                /*RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams) tabLine.getLayoutParams();
                //获取组件距离左侧组件的距离
                lp.leftMargin=(int) ((positionOffset+position)*screenWidth/4);
                tabLine.setLayoutParams(lp);*/
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        radioButton1.performClick();
    }

    @Override
    public void initTitle() {
        ImageButton titleBack = findViewById(R.id.title_back);
        titleBack.setOnClickListener(this);
        ImageButton topMenu = findViewById(R.id.title_menu1);
        topMenu.setVisibility(View.GONE);
        topMenu.setOnClickListener(this);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_pro_vote_voters_detail);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        radioGroup = findViewById(R.id.radioGroup);
        radioButton1 = findViewById(R.id.radio1);
        radioButton2 = findViewById(R.id.radio2);
        radioButton3 = findViewById(R.id.radio3);
        viewPager = findViewById(R.id.viewpager);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.title_menu1:
                finish();
                break;
        }
    }

    public class ViewPagerAdapter extends FragmentPagerAdapter {
        private ArrayList<Object> items;
        public ViewPagerAdapter(FragmentManager fm,ArrayList<Object> items) {
            super(fm);
            this.items = items;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return (Fragment) items.get(position);
                case 1:
                    return (Fragment) items.get(position);
                case 2:
                    return (Fragment) items.get(position);
            }
            return (Fragment) items.get(position);

        }

        @Override
        public int getCount() {
            return items.size();
        }

    }
}
